package com.tzjt.listview;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ListView lv;
	private StudentDao sDao;
	private String testString;
	private EditText et_name;
	private EditText et_age;
	private RadioGroup rg_sex;
	private RadioButton checkedButton;
	private List<Student> stus;
	private MyAdepter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = ((ListView)findViewById(R.id.lv));
		dbSetup();
		refreshview();

		Button bt = (Button)findViewById(R.id.button1);

		bt.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
            	 et_name=(EditText)findViewById(R.id.et_name);
            	 et_age=(EditText)findViewById(R.id.et_age);
            	 rg_sex = (RadioGroup)findViewById(R.id.sexselect);
            	 checkedButton = (RadioButton)findViewById(rg_sex.getCheckedRadioButtonId());
            	 save(et_name.getText().toString(),et_age.getText().toString(),checkedButton.getText().toString());
            	 testString = "插入成功 ：姓名："+et_name.getText().toString()+"年纪"+et_age.getText().toString()+"性别"+checkedButton.getText().toString();
            	 Toast.makeText(MainActivity.this,  testString, Toast.LENGTH_LONG).show();
            	 refreshview();
         		
             }
         });



	}



	private void refreshview() {
		stus = sDao.getStudentList();
		if(myAdapter==null)
		{
			myAdapter = new MyAdepter();
			lv.setAdapter(myAdapter);
		}else {
			myAdapter.notifyDataSetChanged();
		}
	}

	public void dbSetup() {
		sDao = new StudentDao(this);
	}

	public void save(String name,String age, String sex) {
		Student stu = new Student(name,age, sex);
		sDao.insertData(stu);
	}
	
	class MyAdepter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return stus.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tvTextView=new TextView(MainActivity.this);
			tvTextView.setText("test------"+position+stus.get(position).getName());
			
			return tvTextView;
		}
		
	}

}

